package com.quick.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.quick.common.core.domain.Dict;
import com.quick.common.utils.DictUtils;
import com.quick.common.utils.SecurityUtils;
import com.quick.system.domain.SysDictData;
import com.quick.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quick.system.mapper.SysDictTypeMapper;
import com.quick.system.domain.SysDictType;
import com.quick.system.service.SysDictTypeService;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;

/**
 * 字典类型Service业务层处理
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询字典类型
     *
     * @param dictId 字典类型主键
     * @return 字典类型
     */
    @Override
    public SysDictType selectSysDictTypeByDictId(Long dictId) {
        return sysDictTypeMapper.selectSysDictTypeByDictId(dictId);
    }

    /**
     * 查询字典类型列表
     *
     * @param sysDictType 字典类型
     * @return 字典类型
     */
    @Override
    public List<SysDictType> selectSysDictTypeList(SysDictType sysDictType) {
        return sysDictTypeMapper.selectSysDictTypeList(sysDictType);
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int insertSysDictType(SysDictType sysDictType) {
        sysDictType.setCreateTime(new Date());
        sysDictType.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return sysDictTypeMapper.insertSysDictType(sysDictType);
    }

    /**
     * 修改字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int updateSysDictType(SysDictType sysDictType) {
        sysDictType.setUpdateTime(new Date());
        sysDictType.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return sysDictTypeMapper.updateSysDictType(sysDictType);
    }

    /**
     * 批量删除字典类型
     *
     * @param dictIds 需要删除的字典类型主键
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeByDictIds(Long[] dictIds) {
        return sysDictTypeMapper.deleteSysDictTypeByDictIds(dictIds);
    }

    /**
     * 删除字典类型信息
     *
     * @param dictId 字典类型主键
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeByDictId(Long dictId) {
        return sysDictTypeMapper.deleteSysDictTypeByDictId(dictId);
    }

    /**
     * 查询字典类型列表
     *
     * @return 字典类型集合
     */
    @Override
    public List<Dict> selectSysDictTypeList() {
        List<SysDictType> sysDictTypeList = sysDictTypeMapper.selectSysDictTypeList(new SysDictType());
        List<Dict> dictList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysDictTypeList)) {
            for (SysDictType sysDictData : sysDictTypeList) {
                Dict dict = new Dict();
                dict.setDictValue(sysDictData.getDictType());
                dict.setDictLabel(sysDictData.getDictName());
                dictList.add(dict);
            }
        }
        return dictList;
    }
    @PostConstruct
    public void init() {
        loadDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        loadDictCache();
    }
    private void loadDictCache() {
        DictUtils.clearDictCache();
        SysDictData dictData = new SysDictData();
        dictData.setStatus("0");
        Map<String, List<SysDictData>> dictDataMap = sysDictDataMapper.selectSysDictDataList(dictData).stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet()) {
            List<SysDictData> sysDictDataList = entry.getValue().stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList());
            List<Dict> dictList = new ArrayList<>();
            for (SysDictData sysDictData : sysDictDataList) {
                Dict dict = new Dict();
                dict.setDictValue(sysDictData.getDictValue());
                dict.setDictLabel(sysDictData.getDictLabel());
                dictList.add(dict);
            }
            DictUtils.setDictCache(entry.getKey(), dictList);
        }
    }
}
